package org.lemon.transport.context.service.discovery;

import lombok.extern.slf4j.Slf4j;
import org.lemon.transport.context.utils.RandomStringUtils;
import org.lemon.transport.context.utils.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.UUID;

@Slf4j
@Component
public class ServiceInfoProvider {

    @Value("${service.id:#{null}}")
    private String serviceId;

    @PostConstruct
    public void init() {
        if (StringUtils.isNullOrEmpty(serviceId)){
            try {
                serviceId = InetAddress.getLocalHost().getHostName();
            } catch (UnknownHostException e) {
                serviceId = RandomStringUtils.random(10);
            }
        }
        log.info("Current service ID: {}", serviceId);
    }

    public String getServiceId() {

        return serviceId;
    }
}
